package com.ali.ttt.analysis.controller;

import cn.hutool.core.util.StrUtil;
import com.ali.ttt.analysis.entity.AccessOperateRecord;
import com.ali.ttt.analysis.entity.Person;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author : yuxl
 * @create 2024/7/25 12:09 PM
 */
@Slf4j
@RestController
@RequestMapping("/analysis/person")
@Api(tags = "用户操作")
@AllArgsConstructor
public class PersonController {

    private MongoTemplate mongoTemplate;

    @PutMapping("/add")
    @ApiOperation(value = "创建一个用户")
    public Person add(@RequestBody Person person) {
        person.setTime(LocalDateTime.now());
        return mongoTemplate.insert(person);
    }

    @PutMapping("/list")
    @ApiOperation(value = "查询列表")
    public List<Person> list(@RequestBody Person person) {
        Criteria criteria = new Criteria();
        if(person.getAge()!=null){
            criteria.and("age").gt(person.getAge());
        }
        if(StrUtil.isNotEmpty(person.getName())){
            criteria.and("name").is(person.getName());
        }
        if(person.getTime()!=null){
            criteria.and("time").gt(person.getTime());
        }
        Query q = Query.query(criteria);
//        q.fields().include("age","name");
        q.with(Sort.by(Sort.Order.desc("name")));
        return mongoTemplate.find(q,Person.class);
    }
}
